package com.myshop.controller;


import com.myshop.entity.R;
import com.myshop.entity.User;
import com.myshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户模块的用户实体(User)表控制层
 *
 * @author makejava
 * @since 2023-07-21 16:17:54
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public R login(String name, String password) {
        System.out.println("登录接口：" + name + " " + password);
        User user = userService.queryByName(name);
        if (user == null) {
            return new R()
                    .setCode(201)
                    .setMsg("账号错误！");
        }
        if (!user.getPassword().equals(password)) {
            return new R()
                    .setCode(202)
                    .setMsg("密码错误！");
        }
        user.setPassword(null);
        return new R()
                .setCode(200)
                .setData(user);
    }

    @RequestMapping("/loginMan")
    public R loginMan(String name, String password) {
        System.out.println(name + ":" + password);
        User user = userService.queryByName(name);
        System.out.println(user.getRole());
        if (user == null) {
            return new R()
                    .setCode(201)
                    .setMsg("用户名错误");
        }
        if (!user.getPassword().equals(password)) {
            return new R()
                    .setCode(202)
                    .setMsg("密码错误");
        }
        if (user.getRole() == 1) {
            return new R()
                    .setCode(200)
                    .setMsg("登陆成功");

        } else {
            return new R()
                    .setCode(203)
                    .setMsg("该用户不是管理员");
        }
    }

    @RequestMapping("/registerMan")
    public R registerMan(String name, String password,String sex) {
        System.out.println(name + ":" + password+":"+sex);
        User user = userService.queryByName(name);
        if (user != null) {
            return new R()
                    .setCode(201)
                    .setMsg("用户名重复");
        }
        boolean b = userService.addAdminUser(name, password,sex);
        System.out.println(b);
        if (b) {
            return new R()
                    .setCode(200)
                    .setMsg("注册成功");
        } else {
            return new R()
                    .setCode(202)
                    .setMsg("注册失败");
        }

    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @RequestMapping("/insert")
    public R insert(User user){
        userService.save(user);
        return R.succ(null);
    }


}
